﻿/*
 * Copyright (C) 2011 The Calculator for Android Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.japminh.tool.calculator.model;

import net.japminh.tool.calculator.exception.CalculateTypeFormatException;
import net.japminh.tool.calculator.util.LogUtils;

/**
 * 演算のタイプ
 * 
 * @author thangns
 *
 */
public enum CalculateType {
	/**
	 * 割り算
	 */
	Divide("/"),

	/**
	 * 引き算
	 */
	Minus("-"),

	/**
	 * 掛け算
	 */
	Multiply("*"),

	/**
	 * 足し算
	 */
	Plus("+");

	private String symbol;

	private CalculateType(String symbol) {
		this.symbol = symbol;
	}

	@Override
	public String toString() {
		return symbol;
	}

	/**
	 * 指定された記号からCalculateTypeを生成する
	 * 
	 * @param symbol 指定された記号
	 * @return
	 * @throws CalculateTypeFormatException
	 */
	public static CalculateType parseCalculateType(String symbol) throws CalculateTypeFormatException {
		CalculateType[] calculateTypeValues = CalculateType.values();
		for (CalculateType value : calculateTypeValues) {
			LogUtils.d(value.name() + "(" + value.toString() + " , " + value.ordinal() + ") : "
					+ calculateTypeValues[value.ordinal()] + ", " + CalculateType.valueOf(value.name()));

			if (value.toString().equals(symbol)) {
				return value;
			}
		}
		throw new CalculateTypeFormatException();
	}
}
